package com.we.connect.weconnect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.we.connect.weconnect.mapper.remind.NewsRemindInfoMapper;
import com.we.connect.weconnect.mapper.remind.RemindInfoMapper;
import com.we.connect.weconnect.mapper.remind.TextRemindInfoMapper;
import com.we.connect.weconnect.model.remind.NewsRemindInfo;
import com.we.connect.weconnect.model.remind.RemindInfo;
import com.we.connect.weconnect.model.remind.TextRemindInfo;
import com.we.connect.weconnect.model.remind.vo.RemindInfoVo;
import com.we.connect.weconnect.model.system.SysInfo;
import com.we.connect.weconnect.service.RemindInfoService;
import com.we.connect.weconnect.service.SysInfoService;
import com.we.connect.weconnect.utils.DateUtils;
import com.we.connect.weconnect.utils.ScheduleUtils;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RemindInfoServiceImpl implements RemindInfoService {

    @Autowired
    private RemindInfoMapper remindInfoMapper;

    @Autowired
    private TextRemindInfoMapper textRemindInfoMapper;

    @Autowired
    private NewsRemindInfoMapper newsRemindInfoMapper;

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private SysInfoService sysInfoService;

    @Override
    public Boolean addRemindInfo(RemindInfoVo remindInfoVo) {
        TextRemindInfo textRemindInfo;
        NewsRemindInfo newsRemindInfo;
        RemindInfo remindInfo = new RemindInfo();
        if(remindInfoVo.getType().equals("text")){
            textRemindInfo = new TextRemindInfo();
            textRemindInfo.setContent(remindInfoVo.getContent());
            textRemindInfoMapper.insert(textRemindInfo);
            remindInfo.setRemindId(textRemindInfo.getTableId());
        } else if (remindInfoVo.getType().equals("news")) {
            newsRemindInfo = new NewsRemindInfo();
            newsRemindInfo.setContent(remindInfoVo.getContent());
            newsRemindInfo.setUrl(remindInfoVo.getUrl());
            newsRemindInfo.setPicture(remindInfoVo.getPicture());
            newsRemindInfoMapper.insert(newsRemindInfo);
            remindInfo.setRemindId(newsRemindInfo.getTableId());
        }

        remindInfo.setToUser(remindInfoVo.getToUser());
        remindInfo.setUserId(remindInfoVo.getUserId());
        remindInfo.setTime(remindInfoVo.getTime());
        remindInfo.setType(remindInfoVo.getType());
        remindInfo.setTitle(remindInfoVo.getTitle());
        remindInfo.setStart(remindInfoVo.getStart());
        remindInfo.setCreateBy(String.valueOf(remindInfoVo.getUserId()));
        remindInfo.setCreateTime(DateUtils.getTime());

        int insert = remindInfoMapper.insert(remindInfo);
        if(insert != 1){
            return false;
        }

        // 启动定时
        if(remindInfoVo.getStart() == '1'){
            // 查询完整的RemindInfoVo和sysInfo
            RemindInfoVo tempRemindInfoVo = seeRemindInfo(remindInfo.getTableId());
            SysInfo tempSysInfo = sysInfoService.findSysInfo(remindInfoVo.getUserId());

            try {
                ScheduleUtils.createScheduleJob(scheduler, tempRemindInfoVo, tempSysInfo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        return true;
    }

    @Override
    public Boolean editRemindInfo(RemindInfoVo remindInfoVo) {
        TextRemindInfo textRemindInfo;
        NewsRemindInfo newsRemindInfo;
        RemindInfo remindInfo = new RemindInfo();

        if(remindInfoVo.getType().equals("text")){
            textRemindInfo = new TextRemindInfo();
            textRemindInfo.setContent(remindInfoVo.getContent());
            QueryWrapper<TextRemindInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("table_id", remindInfoVo.getRemindId());

            TextRemindInfo isE = textRemindInfoMapper.selectOne(wrapper);
            if(null == isE){
                textRemindInfoMapper.insert(textRemindInfo);
                remindInfo.setRemindId(textRemindInfo.getTableId());
            }else {
                textRemindInfoMapper.update(textRemindInfo, wrapper);
            }

        } else if (remindInfoVo.getType().equals("news")) {
            newsRemindInfo = new NewsRemindInfo();
            newsRemindInfo.setContent(remindInfoVo.getContent());
            newsRemindInfo.setUrl(remindInfoVo.getUrl());
            newsRemindInfo.setPicture(remindInfoVo.getPicture());
            QueryWrapper<NewsRemindInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("table_id", remindInfoVo.getRemindId());

            NewsRemindInfo isE = newsRemindInfoMapper.selectOne(wrapper);
            if(null == isE){
                newsRemindInfoMapper.insert(newsRemindInfo);
                remindInfo.setRemindId(newsRemindInfo.getTableId());
            }else {
                newsRemindInfoMapper.update(newsRemindInfo, wrapper);
            }

        }

        remindInfo.setToUser(remindInfoVo.getToUser());
        remindInfo.setUserId(remindInfoVo.getUserId());
        remindInfo.setTime(remindInfoVo.getTime());
        remindInfo.setType(remindInfoVo.getType());
        remindInfo.setTitle(remindInfoVo.getTitle());
        remindInfo.setStart(remindInfoVo.getStart());
        remindInfo.setUpdateBy(String.valueOf(remindInfoVo.getUserId()));
        remindInfo.setUpdateTime(DateUtils.getTime());
        remindInfo.setTableId(remindInfoVo.getTableId());

        QueryWrapper<RemindInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("table_id", remindInfoVo.getTableId());


        int update = remindInfoMapper.update(remindInfo, wrapper);
        if(update != 1){
            return false;
        }

        // 删除现在的任务，创建新的任务
        JobKey jobKey = JobKey.jobKey(String.valueOf(remindInfoVo.getTableId()));
        try {
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }

        if(remindInfoVo.getStart() == '1'){
            // 查询完整的RemindInfoVo和sysInfo
            RemindInfoVo tempRemindInfoVo = seeRemindInfo(remindInfo.getTableId());
            SysInfo tempSysInfo = sysInfoService.findSysInfo(remindInfoVo.getUserId());

            try {
                ScheduleUtils.createScheduleJob(scheduler, tempRemindInfoVo, tempSysInfo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

        return true;

    }

    @Override
    public Boolean deleteRemindInfo(Long id) {

        // 删除任务
        JobKey jobKey = JobKey.jobKey(String.valueOf(id));
        try {
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e) {
            return false;
        }

        QueryWrapper<RemindInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("table_id", id);
        RemindInfo remindInfo = remindInfoMapper.selectOne(wrapper);

        // 删除子表
        if(remindInfo.getType().equals("text")){
            QueryWrapper<TextRemindInfo> childWrapper = new QueryWrapper<>();
            childWrapper.eq("table_id", remindInfo.getRemindId());
            textRemindInfoMapper.delete(childWrapper);
        } else if (remindInfo.getType().equals("news")) {
            QueryWrapper<NewsRemindInfo> childWrapper = new QueryWrapper<>();
            childWrapper.eq("table_id", remindInfo.getRemindId());
            newsRemindInfoMapper.delete(childWrapper);
        }

        int delete = remindInfoMapper.delete(wrapper);
        if(delete != 1){
            return false;
        }
        return true;
    }

    @Override
    public List<RemindInfo> findRemindInfoVoList(Long userId) {
        QueryWrapper<RemindInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<RemindInfo> remindInfoList = remindInfoMapper.selectList(wrapper);

        for (RemindInfo remindInfo : remindInfoList){
            String toUser = remindInfo.getToUser();
            String[] split = toUser.split("-");
            remindInfo.setToUser(split[1]);
        }

        return remindInfoList;
    }

    @Override
    public RemindInfoVo seeRemindInfo(Long id) {
        RemindInfoVo remindInfoVo = new RemindInfoVo();
        QueryWrapper<RemindInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("table_id", id);
        RemindInfo remindInfo = remindInfoMapper.selectOne(wrapper);

        remindInfoVo = conversion(remindInfoVo, remindInfo);

        return remindInfoVo;
    }

    @Override
    public List<RemindInfoVo> getALLRemindInfoVoList() {
        ArrayList<RemindInfoVo> result = new ArrayList<>();

        QueryWrapper<RemindInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("start", '1');
        List<RemindInfo> remindInfoList = remindInfoMapper.selectList(wrapper);

        for(RemindInfo remindInfo : remindInfoList){
            RemindInfoVo remindInfoVo = new RemindInfoVo();
            remindInfoVo = conversion(remindInfoVo, remindInfo);
            result.add(remindInfoVo);
        }

        return result;
    }

    private RemindInfoVo conversion(RemindInfoVo remindInfoVo, RemindInfo remindInfo){
        remindInfoVo.setTableId(remindInfo.getTableId());
        remindInfoVo.setUserId(remindInfo.getUserId());
        remindInfoVo.setToUser(remindInfo.getToUser());
        remindInfoVo.setTime(remindInfo.getTime());
        remindInfoVo.setType(remindInfo.getType());
        remindInfoVo.setTitle(remindInfo.getTitle());
        remindInfoVo.setStart(remindInfo.getStart());
        remindInfoVo.setRemindId(remindInfo.getRemindId());

        if(remindInfoVo.getType().equals("text")){
            QueryWrapper<TextRemindInfo> cWrapper = new QueryWrapper<>();
            cWrapper.eq("table_id", remindInfo.getRemindId());
            TextRemindInfo textRemindInfo = textRemindInfoMapper.selectOne(cWrapper);
            remindInfoVo.setContent(textRemindInfo.getContent());
        }else {
            QueryWrapper<NewsRemindInfo> cWrapper = new QueryWrapper<>();
            cWrapper.eq("table_id", remindInfo.getRemindId());
            NewsRemindInfo newsRemindInfo = newsRemindInfoMapper.selectOne(cWrapper);
            remindInfoVo.setContent(newsRemindInfo.getContent());
            remindInfoVo.setUrl(newsRemindInfo.getUrl());
            remindInfoVo.setPicture(newsRemindInfo.getPicture());
        }

        return remindInfoVo;
    }
}
